package com.eonmind.vip.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.eonmind.common.contract.data.response.ResponseEntity;
import com.eonmind.common.provider.entity.vip.Subscription;
import com.eonmind.common.provider.entity.vip.UserWelfare;
import com.eonmind.user.contract.dto.UserInfoData;
import com.eonmind.vip.dto.UserWelfareDto;
import com.eonmind.vip.mapper.SubscriptionMapper;
import com.eonmind.vip.mapper.UserWelfareMapper;
import com.eonmind.vip.provider.VipToUserServerProvider;
import com.eonmind.vip.service.WelfareService;
import com.eonmind.vip.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@Service
public class WelfareServiceImpl implements WelfareService {

    @Autowired
    private UserWelfareMapper userWelfareMapper;

    @Autowired
    private SubscriptionMapper subscriptionMapper;

    @Autowired
    private VipToUserServerProvider vipToUserServerProvider;


    @Override
    public ResponseEntity<UserWelfareDto.UserWelfareResponse> getWelfareInfo(UserInfoData userInfoData) {
        UserWelfare userWelfare = userWelfareMapper.getUserPlanByUserID(userInfoData.getId());
        if (ObjectUtil.isEmpty(userWelfare)) {
            vipToUserServerProvider.initUserWelfare(userInfoData.getId());
            userWelfare = userWelfareMapper.getUserPlanByUserID(userInfoData.getId());
        }
        UserWelfareDto.UserWelfareResponse userWelfareResponse = new UserWelfareDto.UserWelfareResponse()
                .setUserWelfare(userWelfare)
                .setUsername(userInfoData.getUserName())
                .setAvatar(userInfoData.getHeadshot())
                .setMemberLevel("砖石");
        List<Subscription> active = subscriptionMapper.findAllActiveSubsByUserId("active", userInfoData.getId());
        userWelfareResponse.setIsMember(!ObjectUtil.isEmpty(active));
        Long endDate = 0L;
        Optional<Subscription> max = active.stream().max(Comparator.comparingLong(Subscription::getEndDate));
        if (!ObjectUtil.isEmpty(active)) {
            endDate = max.get().getEndDate();
            userWelfareResponse.setActiveTime(TimeUtil.LongToString(endDate));
        }
        userWelfareResponse.setIsExpired(subscriptionMapper.findIsHaveExpired("expired", userInfoData.getId()));
        return ResponseEntity.success(userWelfareResponse);
    }

    @Override
    public ResponseEntity<UserWelfareDto.UserPointsResponse> getPointsInfo(UserInfoData userInfoData) {
        if (ObjectUtil.isEmpty(userInfoData)) return ResponseEntity.fail("获取失败");
        UserWelfare userWelfare = userWelfareMapper.getUserPlanByUserID(userInfoData.getId());
        if (ObjectUtil.isEmpty(userWelfare)) {
            vipToUserServerProvider.initUserWelfare(userInfoData.getId());
            userWelfare = userWelfareMapper.getUserPlanByUserID(userInfoData.getId());
        }
        if (ObjectUtil.isEmpty(userWelfare.getCachedFeatures())) {
            return ResponseEntity.fail("获取失败");
        }
        UserWelfareDto.UserPointsResponse userPointsResponse = new UserWelfareDto.UserPointsResponse()
                .setUsedPoints(ObjectUtil.isEmpty(userWelfare.getCachedFeatures().getUsedCoins()) ? 0 : userWelfare.getCachedFeatures().getUsedCoins())
                .setTotalPoints(userWelfare.getCachedFeatures().getAllCoins() == null ? 0 : userWelfare.getCachedFeatures().getAllCoins())
                .setCurrentPoints(userWelfare.getCachedFeatures().getTotalCoins() == null ? 0 : userWelfare.getCachedFeatures().getTotalCoins());
        return ResponseEntity.success(userPointsResponse);
    }
}
